package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.SortDetailBo;
import com.tpyule.tpclass.entity.dto.CommonQueryDto;
import com.tpyule.tpclass.entity.pojo.Direction;
import com.tpyule.tpclass.entity.pojo.Sort;
import com.tpyule.tpclass.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @date 2020/2/26 0026 下午 3:45
 */
@RestController
@RequestMapping("sort")
public class SortController extends BaseController {
    @Autowired
    SortService sortService;

    @RequestMapping("getAllSort")
    @ResponseBody
    public void getAllSort(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        List<Sort> sorts=sortService.getAllSort();
        if(sorts!=null){
            resultBean=new SuccessResultBean<List<Sort>>(sorts,"获取成功");
        }else{
            resultBean=new FailResultBean<>("获取失败");
        }
        try {
            this.setAjaxResponseResult(response,resultBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("getSortByDirection")
    @ResponseBody
    public void getSortByDirection(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String directionI=request.getParameter("directionId");
        int directionId = 0;
        if(directionI!=null){
            directionId = Integer.parseInt(directionI);
        }

        List<Sort> sorts=sortService.getSortByDirectionId(directionId);
        if(sorts!=null){
            resultBean=new SuccessResultBean<List<Sort>>(sorts,"获取成功");
        }else{
            resultBean=new FailResultBean<>("获取失败");
        }
        try {
            this.setAjaxResponseResult(response,resultBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getSizeSort")
    @ResponseBody
    public void getSizeSort(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;

        int result=sortService.getSizeSort();
        resultBean=new SuccessResultBean(result,"获取成功");

        try {
            this.setAjaxResponseResult(response,resultBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("getListSort")
    @ResponseBody
    public void getListSort(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        try {
            String queryStr = request.getParameter("queryData");
            if(queryStr!=null){
                CommonQueryDto commonQueryDto = objectMapper.readValue(queryStr,CommonQueryDto.class);
                if (commonQueryDto!=null){
                    List<SortDetailBo> sortDetailBos = sortService.getListSort(commonQueryDto);
                    if(sortDetailBos!=null){
                        resultBean = new SuccessResultBean(sortDetailBos, "成功");
                    }else {
                        resultBean = new FailResultBean("数据错误");
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("update")
    @ResponseBody
    public void updateSort(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        Sort sort = null;
        try {
            String sortStr=request.getParameter("sortData");
            if(sortStr!=null){
                sort = objectMapper.readValue(sortStr, Sort.class);
                int result = sortService.updateSort(sort);
                if (result>0){
                    resultBean = new SuccessResultBean("成功！");
                }else {
                    resultBean = new FailResultBean("失败");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/add")
    @ResponseBody
    public void addSort(HttpServletRequest request,HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean=null;
        Sort sort = null;
        try {
            String sortStr=request.getParameter("sortData");
            if(sortStr!=null){
                sort = objectMapper.readValue(sortStr, Sort.class);
                int result = sortService.addSort(sort);
                if (result>0){
                    resultBean = new SuccessResultBean("成功！");
                }else {
                    resultBean = new FailResultBean("失败");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    /**
     * 删除分类
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("delete")
    @ResponseBody
    public void deleteSort(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        Sort sort = null;
        try {
            String sortStr=request.getParameter("sortData");
            if(sortStr!=null){
                sort = objectMapper.readValue(sortStr, Sort.class);
                resultBean = sortService.deleteSort(sort);
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
}
